
% inputvideo = 'V3V100013_010.crop.avi'; %'video.avi';
% inputvideo = 'R:\fourier\Srinidhi\sample_videos\V3V100004_005_512x512.avi'; %512x512 Vivid
inputvideo = 'R:\fourier\Srinidhi\sample_videos\BaslerCapture_2011_05_10.avi';

VISUALIZE = 0;  %0 : no visualization validate output file, 1: visualize matlab output

%scan the video
  mov = aviread(inputvideo) ; 
  
nr_frame = size(mov(1,1).cdata,1); %512;
nc_frame = size(mov(1,1).cdata,2); %512;

nr = 256; %128; %256
nc = 256; %128; %256;


Nc = 3; %1; % num of center channels

% bin_dat_bar = 1;     %0 : dat file (ascii), 1 : binary file

output_nr = 16; %8; %16; % size of output saliency
output_nc = 16; %8; %16;
%% for comparing hardware result
result_file = 'saliency_IOMF_1.dat'; %1-o_BAK.bin'; %'sal_IOM_128_out.bin'; %'motion_result.dat';  %'../saliency_ml605/orientation_channel_result.dat'; 
error_threshold = 1e-3;
result_flag = 1;

NUM_FRAMES = 100; %size(mov,2); %201; %100; %201;
bin_dat_bar = 1;     %0 : dat file (ascii), 1 : binary file
% res_fname_0 = '1-o_BAK.bin';
% res_fname_1 = '2-o_BAK.bin';
if (bin_dat_bar == 1)
    outfilepath=  'R:\fourier\MSPark\Saliency API\NeoEnvironment\Applications\streaming_loopback_v1_00_a\test_result_6\'; %'results\saliency_retina_dma\'; 
    %'R:\fourier\MSPark\Saliency API\NeoEnvironment\Applications\streaming_loopback_v1_00_a\Data256\'; %'R:\kesturvy\NEO_VISION\DNDUALV6_PCIE4\demo_oct2010\Data256\'; %'..\..\..\demo_dec2010\Data\';
%     outfilepath = 'R:\fourier\MSPark\HRL object percept detection API\NeoEnvironment\Applications\streaming_loopback_v1_00_a\test_result_6\';
else
    outfilepath = 'results\saliency_retina\'; %'results\beta_CFIOM_6cs_256\';
end

input_BINfile = 0; % if input is to be read from binary file
logfile = 'output.log';

if(VISUALIZE == 1)  %if visualization enabled
    scrsz= get(0,'ScreenSize');
%     h = figure('position',[200 scrsz(4)/3 scrsz(3)/2 scrsz(4)/3]);
    h = figure('position',[10 scrsz(4)/3 scrsz(3) scrsz(4)/3]);
       
    p0 = subplot(1,3,1);
    axis equal
    %%set(gca,'nextplot', 'replacechildren');
    p1 = subplot(1,3,2);
    axis equal    
    p2 = subplot(1,3,3);
    axis equal    

%     p0 = subplot(1,2,1);
%     axis equal
%     p2 = subplot(1,2,2);
%     axis equal       

end

logfile_id = fopen(logfile,'w+');   %open the log file
qout = quantizer('fixed', [24,20]);  %output data rep
qin = quantizer('ufixed',[8,0]); % for r, g, b values

im_curr = zeros(nr,nc,3);
im_prev = zeros(nr,nc,3);

% breaking frame into chips
cpf_r = floor(nr_frame/nr); % num of chips per frame
cpf_c = floor(nc_frame/nc); % num of chips per frame
cpf = cpf_r * cpf_c;

rs = 0;
cs = 0;
re = nr;
ce = nc; %256

r_start = zeros(1,cpf);
c_start = zeros(1,cpf);
r_end = zeros(1,cpf);
c_end = zeros(1,cpf);

% start and end for every chip in the frame
for j = 1:cpf_r
    for k = 1:cpf_c        
        c_start(j,k) = cs + (k-1)*nc;
        c_end(j,k) = ce + (k-1)*nc;
        r_start(j,k) = rs + (j-1)*nr;
        r_end(j,k) = re + (j-1)*nr;
    end
end


  
 out_mov(1:NUM_FRAMES) = struct('cdata', [],'colormap', []);
                        
    for i = 1:NUM_FRAMES  %size(mov,2) %will go to number of frames 
       for cp = 1:CHIPS_PER_FRAME
    
%             if(i == 1)
%                 im_prev = zeros(nr,nc,3); %(size(mov(1,i).cdata));
%             else
%                 im_prev = frame;
%             end
%             frame = mov(1,i).cdata;
%             im_curr = frame;
            
            im_prev = mov(1,i-1).cdata;
            im_curr = mov(1,i).cdata;

     
    if(input_BINfile == 1) 
        [Sal, iSal, cSal, oSal, mSal, fSal] = saliency_hw_sim(im_prev, im_curr, nr, nc, Nc, i+1);
    else    
        [Sal, iSal, cSal, oSal, mSal, fSal] = saliency_hw_sim(im_prev, im_curr, nr, nc, Nc, i);      
    end
    

    if(bin_dat_bar == 1)    %binary file
        if(input_BINfile == 1)    %binary file
            out_filename=strcat(outfilepath,num2str(i  ),'-','o_BAK','.','bin');     %check file name i-o-BAK.bin exixts
        else
%             out_filename=strcat(outfilepath,num2str(i-1),'-','o_BAK','.','bin');     %check file name i-o-BAK.bin exixts
            
%             out_filename=strcat(outfilepath, num2str(i-2),'-','TargetBuffer','.','bin');     %check file name i-o-BAK.bin exixts            
            out_filename=strcat(outfilepath,'0C',num2str(i-1),'F','-','TargetBuffer','.','bin');     %check file name i-o-BAK.bin exixts            
        end
        check_file =0;
        while (check_file ~= 2)
            check_file= exist (out_filename, 'file');
        end
        file_id = fopen(out_filename,'r');
        Bdec=dec2hex(fread(file_id,inf,'uint32'));
        array_out= hex2num(qout,Bdec); 
        fclose(file_id);
        result_hw = reshape(array_out, output_nc, output_nr)';
    elseif(bin_dat_bar == 0)  %dat file
        out_filename=strcat(outfilepath,num2str(i),'-','o_BAK','.','dat');     %check file name i-o-BAK.bin exixts
        check_file =0;
        while (check_file ~= 2)
            check_file= exist (out_filename, 'file');
        end
        array_out = dat2array(out_filename, output_nr*output_nc, qout);          
        result_hw = reshape(array_out, output_nc, output_nr)';
    end

    if(VISUALIZE)
        axes(p0)
        imagesc(imresize(im_curr, [nr,nc]));
%         title (['Input Frame  ']);
        title (['Input Frame  ', num2str(i)]);

        axes(p1)
        imagesc(imresize(Sal, [nr,nc]));        %Visualize REF output
        title (['Ref Saliency  ', num2str(i)]);

        axes(p2)
        imagesc(imresize(result_hw,[nr,nc]));   %Visualize HARDWARE output
%         title (['FPGA Saliency  ']);
        title (['FPGA Saliency  ', num2str(i)]); 
        
        pause(0.01) %0.25
  %      out_mov(i)= getframe(h);
    end
    
    abs_error = abs((Sal/5) - (result_hw/5));
    max_error = max(max(abs_error));
    avg_error = mean(mean(abs_error));       

    error_flag = abs_error > error_threshold;
    num_error = sum(sum(error_flag));
    error_flag = error_flag * 1111;


    fprintf(logfile_id,'Frame %d - num error = %d, max error = %E, avg error = %E\n', i, num_error, max_error, avg_error);       
    
    end
 end
   
%close the log file
 fclose(logfile_id);

 %% Make movie 
%  close(h);
 %movie2avi(out_mov, 'saliency_6cs_256_SX475.avi', 'compression', 'None');
 
        
